package com.zcsy.seller.web.system;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.manage.model.system.Log;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.system.ILogService;
import com.zcsy.manage.service.system.IUserService;

/**
 * @Title: LogController.java
 * @Package： com.zcsy.manage.web.system 
 * @Description: 系统日志控制层
 *  〈功能详细描述〉
 *
 * @author caiyinli 13277030520@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年1月14日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/log")
public class LogController {
	
    private static final int PAGE_SIZE =  10; //每页大小
    private static final int PAGE_NO   =  1;  //默认首页
	
	@Autowired
	private ILogService logService;
	
	@Autowired 
	private IUserService userService;
	
	 /**
	 * @Description 根据条件查询用户操作日志记录<br>
	 * 
	 * @param pageNo 当前页
	 * @param pageSize 每页大小
	 * @param userName 用户名称
	 * @param beginDate 开始日期	
	 * @param endDate 结束日期
	 * @param req 用户请求
	 * @return 日志集合
	 * @author caiyinli 13277030520@163.com
	 * @date 2015年8月24日
	 * @see 
	 */
    @RequestMapping(value="logList" , method = RequestMethod.GET)
    public String logList(Model model, Integer pNo,String userName,String beginDate,String endDate, HttpServletRequest req){
    	
        List<String> memIds = new ArrayList<String>();
        
        if(userName != null){
            User user = new User();
            user.setLoginname(userName);
            user.setState(0);
            List<User> userList = userService.queryUser(user);
            if(userList != null && userList.size()>0){
                for(User mem : userList){
                    memIds.add(mem.getId());
                }
            }
        }
       
	    if(pNo == null){
            pNo = PAGE_NO;
        }
	    
	    if(userName == null){
	    	userName = null;
	    }
	    
        PageHelper.startPage(pNo, PAGE_SIZE);
        
        Log record = new Log();
        if(memIds != null && memIds.size()>0){
            record.setUserIds(memIds);
        }
        if(StringUtils.isNotEmpty(beginDate)){
        	record.setBeginDate(beginDate);
        }
        
        if(StringUtils.isNotEmpty(endDate)){
        	record.setEndDate(endDate);
        }
        
        List<Log> logs = logService.getByPage(record);
    	   for(Log log : logs){
    			   String name = userService.selectByPrimaryKey(log.getUserId()).getLoginname();
    			   log.setUserName(name);
    	   }
    	   
        PageInfo<Log> info = new PageInfo<Log>(logs);
        
        
		model.addAttribute("logList", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("userName", userName);
		model.addAttribute("beginDate", beginDate);
		model.addAttribute("endDate", endDate);
        
        return "system/log/logList";
    }
	
}
